<!-- 蓝色登录页面2 -->
<template>
	<view class="total">
		<view class="img-a">
			<view class="header">
				<view class="return" @click="goBack()">
					<image style="width: 30px;height: 30px;margin-top: 10px;" src="/static/return.png" alt="" />
				</view>
			</view>
		</view>
		<view class="login-view">
			<view class="t-login">
				<view class="welcome">
					欢迎登录候鸟养老
				</view>
				<view class="log_style">
					<view class="blue_font">
						机构认证
					</view>
				</view>
				<view class="pic">
					<view class="title">
						请上传营业执照
					</view>
					<w-compress ref='wCompress' />
					<view class="uplode">
						<u-upload :fileList="fileList1" style="flex:0" @afterRead="afterRead" @delete="deletePic"
							name="1" :multiple="false" :maxCount="1" width="220" height="150">
							<image style="width: 220px;height: 150px;" src="@/static/img/card_btn_license.png" alt=""
								width="100%" height="100%">
						</u-upload>
					</view>
				</view>
				<form class="cl">
					<view class="t-a">
						<input name="phone" placeholder="机构名称" maxlength="11" v-model="form.companyName" />
					</view>
					<view class="t-a">
						<input name="code" maxlength="18" placeholder="成立时间" v-model="form.RegistrationDate" />
					</view>
					<view class="t-a">
						<input name="code" maxlength="18" placeholder="社会统一信用代码" v-model="form.creditCode" />
					</view>
					<view class="t-a">
						<input name="code" maxlength="18" placeholder="法人姓名" v-model="form.legalPerson" />
					</view>
					<view class="t-a">
						<input name="code" maxlength="18" placeholder="法人身份证" v-model="form.legalPersonCard" />
					</view>
					<view class="t-a">
						<input name="code" maxlength="18" type="number" placeholder="法人手机号"
							v-model="form.legalPersonTel" />
					</view>
					<view class="t-a">
						<input name="code" maxlength="18" placeholder="经营者姓名" v-model="form.proprietorName" />
					</view>
					<view class="t-a">
						<input name="code" maxlength="18" placeholder="经营者身份证" v-model="form.proprietorCard" />
					</view>
					<view class="t-a">
						<input name="code" maxlength="18" type="number" placeholder="经营者手机号"
							v-model="form.proprietorTel" />
					</view>
					<button class="login_button" @tap="toNext()">确 定</button>
				</form>
			</view>
		</view>
		<view class="" style="height: 100rpx;">

		</view>
	</view>
</template>
<script>
	import WCompress from '@/components/w-compress/w-compress.vue'
	import {
		recognize,
		addLicense
	} from '@/utils/api.js'

	export default {
		components: {
			WCompress
		},
		data() {
			return {
				phone: '', //手机号码
				phone2: '',
				newpwd: '', //忘记密码新密码
				newpwd2: '', //忘记密码新密码确认
				code: '',
				tips: '发送验证码',
				seconds: 60,
				beAgreen: true,
				baseFormData: {
					hobby: [5],
				},
				status: false,
				dataList: '',
				form: {
					companyName: '',
					RegistrationDate: '',
					creditCode: '',
					legalPerson: '',
					legalPersonCard: '',
					legalPersonTel: '',
					proprietorName: '',
					proprietorCard: '',
					proprietorTel: ''
				},
				fileList1: [],
			};
		},
		onLoad(option) {
			try {
				const value = uni.getStorageSync('phone');
				if (value) {
					console.log(value);
					this.phone2 = value
				}
			} catch (e) {
				// error
			}
			console.log(option, 'option');
			if (option.id == 2) {
				this.status = true
			}
		},
		methods: {
			toNext() {
				if (this.dataList === '') {
					uni.showToast({
						title: '请传入正确格式的营业执照',
						icon: 'none'
					})
					return
				}
				if (this.form.companyName === '') {
					uni.showToast({
						title: '请输入机构名称',
						icon: 'none'
					})
					return
				}
				if (this.form.RegistrationDate === '') {
					uni.showToast({
						title: '请输入成立时间',
						icon: 'none'
					})
					return
				}
				if (this.form.creditCode === '') {
					uni.showToast({
						title: '请输入社会统一信用代码',
						icon: 'none'
					})
					return
				}
				if (this.form.legalPerson === '') {
					uni.showToast({
						title: '请输入法人姓名',
						icon: 'none'
					})
					return
				}
				if (this.form.legalPersonCard === '') {
					uni.showToast({
						title: '请输入法人身份证',
						icon: 'none'
					})
					return
				}
				if (this.form.legalPersonTel === '') {
					uni.showToast({
						title: '请输入法人手机号',
						icon: 'none'
					})
					return
				}
				if (this.form.proprietorName === '') {
					uni.showToast({
						title: '请输入经营者姓名',
						icon: 'none'
					})
					return
				}
				if (this.form.proprietorCard === '') {
					uni.showToast({
						title: '请输入经营身份证',
						icon: 'none'
					})
					return
				}
				if (this.form.proprietorTel === '') {
					uni.showToast({
						title: '请输入经营者手机号',
						icon: 'none'
					})
					return
				}
				let data = {
					...this.form,
					...this.dataList
				}
				data.admin_phone = this.phone2
				addLicense(data).then((res) => {
					if (res.data.meta.status == 200) {
						uni.showToast({
							title: '营业执照上传成功！',
							icon: 'none'
						});
						uni.setStorageSync('organization', this.form.companyName)
						if (this.status) {
							setTimeout(() => {
								uni.navigateBack()
							}, 1000)
						} else {
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/login/setPWD'
								})
							}, 1000)
						}

					} else {
						uni.showToast({
							title: res.data.meta.msg,
							icon: 'none'
						});
					}
				})
			},
			uploadFilePromise(urls) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'https://www.ghhlkj.com/api/saveFile',
						filePath: urls,
						name: 'file',
						success: (res) => {
							let data = JSON.parse(res.data) //最终传给的是字符串，这里需要转换格式
							console.log(data, 'data');
							resolve(data)
							let formData = {
								Url: data.url
							}
							recognize(formData).then((res) => {
								console.log(res, '---------');
								if (res.data.meta.status == 200) {
									this.form.companyName = res.data.data.companyName
									this.form.RegistrationDate = res.data.data.RegistrationDate
									this.form.creditCode = res.data.data.creditCode
									this.form.legalPerson = res.data.data.legalPerson
									this.dataList = res.data.data
								} else {
									uni.showToast({
										title: '识别失败，请重试！',
										icon: 'error'
									})
								}
							})
						}
					});
				})
			},
			async afterRead(event) {
				console.log(event)
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				// lists = this.compress(lists)
				console.log(lists,'00000');
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			deletePic(e) {
				console.log(e);
				this[`fileList${e.name}`].splice(e.index, 1)
			},
			compress(files) { //图片压缩并上传
				const that = this
				console.log(that, '-----');
				let compressFile = []
				files.forEach(e => { //循环压缩并上传
					console.log(that.$refs, '//////////////');
					that.$refs.wCompress.start(e, { //调用压缩组件方法
							// pixels: 630000,  // 最大分辨率，默认二百万
							quality: 0.8, // 压缩质量，默认0.8     
							base64: true, // 是否返回base64，默认false，非H5有效
						})
						.then(res => {
							// console.log(res) //base64格式
							this.uploadFilePromise(res) //调用上传方法
						})
						.catch(e => {
							console.log(e)
						})
				})
			},
			goBack() {
				uni.navigateBack()
			},
			change(e) {
				console.log('change', e);
			},
			changeAgreen() {
				this.beAgreen = !this.beAgreen
			},
		}
	};
</script>
<style lang="scss" scoped>
	.total {
		// padding-top: 20px;
		height: 100%;
		background-color: #f6f6f6;
		position: relative;
	}

	.codeInput {
		height: 33px;
		margin-top: 30rpx;
		margin-bottom: 50rpx;
		border: 1px solid #e9e9e9;
		border-radius: 8px;
		background-color: #F6F6F6;
		font-size: 28rpx;
		padding: 0 15px;
	}

	#getCodeButton {
		margin-top: 0px;
		font-size: 5px;
		width: 80px;
		background-color: #2796f2;
		border-color: #2796f2;
		border-radius: 24px;
		height: 28px;
	}

	.txt {
		font-size: 32rpx;
		font-weight: bold;
		color: #333333;
	}

	.img-a {
		width: 100%;
		height: 450rpx;
		padding-top: 20px;
		background-image: url('/static/img/login_img_bgs.png');
		background-repeat: no-repeat;
		background-size: 100%;

		.header {
			height: 50px;
			line-height: 50px;
			font-size: 20px;
			font-weight: 400;
			color: #FFFFFF;
			display: flex;
			justify-content: space-between;
			padding: 0px 20px;

			.return {
				width: 30px;
				line-height: 50px;
				font-size: 5px;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
	}

	.reg {
		font-size: 28rpx;
		color: #fff;
		height: 90rpx;
		line-height: 90rpx;
		border-radius: 50rpx;
		font-weight: bold;
		background: #f5f6fa;
		color: #000000;
		text-align: center;
		margin-top: 30rpx;
	}

	.login-view {
		background: #FFFFFF;
		border-radius: 5px;
		width: 94%;
		margin: auto;
		top: 0px;
		left: 0;
		right: 0;
		bottom: 0;
		margin-top: -150px;
		padding-bottom: 100rpx;

	}

	.t-login {
		width: 600rpx;
		margin: 0 auto;
		font-size: 28rpx;
		padding-top: 80rpx;

		.welcome {
			color: #242424;
			font-size: 25px;
			font-weight: bolder;
		}

		.log_style {
			margin-top: 25px;
			margin-bottom: 30px;

			.blue_font {
				color: #2796f2;
				font-size: 18px;
				font-family: PingFang SC;
				font-weight: 800;
			}

			.tishi {
				margin-top: 10px;
				margin-bottom: 50px;
				font-size: 12px;
				font-family: PingFang SC;
				font-weight: 500;
				color: #868686;
			}
		}

		.pic {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;

			.title {
				// margin-top: 0rpx;
				margin-bottom: 60rpx;
				font-size: 32rpx;
				font-weight: bold;
				color: #484848;
			}

			.uplode {
				margin-bottom: 70rpx;
			}
		}
	}

	.t-login button {
		margin-top: 100rpx;
		font-size: 35rpx;
		background: #2796f2;
		color: #fff;
		height: 90rpx;
		width: 200px;
		line-height: 90rpx;
		border-radius: 50rpx;
		font-weight: bold;
	}

	.t-login input {
		height: 90rpx;
		line-height: 90rpx;
		margin-bottom: 16px;
		border: none;
		border-radius: 8rpx;
		background-color: #F6F6F6;
		font-size: 32rpx;
		padding: 0 15px;
	}

	.t-login .t-a {
		position: relative;
	}

	.t-b {
		text-align: left;
		font-size: 42rpx;
		color: #ffffff;
		padding: 130rpx 0 0 70rpx;
		font-weight: bold;
		line-height: 70rpx;
	}

	.t-login .t-c {
		position: absolute;
		right: 22rpx;
		top: 22rpx;
		background: #5677fc;
		color: #fff;
		font-size: 24rpx;
		border-radius: 50rpx;
		height: 50rpx;
		line-height: 50rpx;
		padding: 0 25rpx;
	}

	.t-login .t-d {
		text-align: center;
		color: #999;
		margin: 80rpx 0;
	}

	.t-login .t-e {
		text-align: center;
		width: 250rpx;
		margin: 80rpx auto 0;
	}

	.t-login .t-g {
		float: left;
		width: 50%;
	}

	.t-login .t-e image {
		width: 50rpx;
		height: 50rpx;
	}

	.t-login .t-f {
		text-align: center;
		margin: 150rpx 0 0 0;
		color: #666;
	}

	.t-login .t-f text {
		margin-left: 20rpx;
		color: #aaaaaa;
		font-size: 27rpx;
	}

	.t-login .uni-input-placeholder {
		color: #aeaeae;
	}

	.cl {
		zoom: 1;

		.forger {
			float: right;
			margin-top: -10px;
		}

		.agreen {
			margin-top: 65px;
			display: flex;

			.radios {
				width: 28px;
				height: 20px;
				margin-right: 10px;
			}

			.texts {
				.txts {
					color: #5398F6;
				}
			}

		}
	}

	.cl:after {
		clear: both;
		display: block;
		visibility: hidden;
		height: 0;
		content: '\20';
	}
</style>